import use_user from "@/stores/use_user";
// export const 登录页 = '/pages/user/login'
// url如果少了 "/" 错误可别看不懂
export const 登录页 = '/uni_modules/uni-id-pages/pages/login/login-withpwd'

export const myRouter = (url, isRedirectTo) => {
	const user = use_user()
	const token = user.userInfo.token || ''
	const 受控页组 = ['/pages/orderInfo/orderInfo', '/pages/tabBar/shop/shop', '/pages/order/order',
		'/pages/tabBar/my/my'
	]

	const loginList = ['/pages/user/login', '/pages/user/register']
	const inBlack = 受控页组.some(b => {
		return url && url.indexOf(b) > -1
	})

	const inLogin = loginList.some(b => {
		return url && url.indexOf(b) > -1
	})

	if (inLogin && token) {
		uni.switchTab({
			url: '/pages/tabBar/index'
		})
		return
	}
	if (inBlack && !token) {
		uni.switchTab({
			url: '/pages/user/login'
		})
		return
	}

	if (isRedirectTo) {
		uni.navigateTo({
			url: url
		})
		return
	}
	// 对url为undefined时做个非空处理
	if (url) {
		uni.switchTab({
			url: url
		})
	}
}